package com.dcivision.dms.web;

import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsRetentionSetup;
import com.dcivision.dms.dao.DmsRetentionSetupDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.SysUserDefinedIndex;
import com.dcivision.framework.dao.SysUserDefinedIndexDAObject;
import com.dcivision.framework.web.AbstractMaintAction;

public class MaintRetentionPeriodSetupAction extends  AbstractMaintAction{
	public static final String REVISION = "$Revision: 1.2.2.1 $";

	public String getFunctionCode() {
		return null;
	}

	public String getMajorDAOClassName() {
		return "com.dcivision.dms.dao.DmsRetentionSetupDAObject";
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
		MaintRetentionPeriodSetupForm actionForm = (MaintRetentionPeriodSetupForm)form;
		Connection conn = this.getConnection(request);
		SessionContainer sessionContainer = this.getSessionContainer(request);
		DmsRetentionSetupDAObject dao = new DmsRetentionSetupDAObject(sessionContainer,conn);
		DmsRetentionSetup bean = null;
		String opMode = actionForm.getOpMode();
		
		//set profile
		Integer objectID = TextUtility.parseIntegerObj(actionForm.getObjectID());
		SysUserDefinedIndexDAObject udfIndexDAO = new SysUserDefinedIndexDAObject(sessionContainer,conn);
		SysUserDefinedIndex udfIndex = (SysUserDefinedIndex)udfIndexDAO.getObjectByID(objectID);
		//joan.xiong update code for bug EIP-1375 by 2007/01/04
		//actionForm.setProfile(udfIndex.getUserDefinedType()); 
		if(!Utility.isEmpty(udfIndex)){
			actionForm.setProfile(udfIndex.getUserDefinedType());
		}
		//joan.xiong update code end
		if (Utility.isEmpty(opMode)){
			try {
				bean =(DmsRetentionSetup)dao.getByObjectID(TextUtility.parseIntegerObj(actionForm.getObjectID()));
				if (Utility.isEmpty(bean)){
					actionForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
					return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
				} else {
					actionForm.setFormData(bean);
					actionForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
					return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
				}
			} catch (ApplicationException e) {
        log.error(e, e);
				return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
			}
		} else if (GlobalConstant.OP_MODE_INSERT.equals(opMode)){
			try {
				super.insertRecord(mapping,actionForm,request,response);
        this.commit(request);
        addMessage(request, MESSAGE_RECORD_INSERT);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
      	actionForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
        return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
      } else {
      	actionForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
        return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
      }			
		} else if (GlobalConstant.OP_MODE_UPDATE.equals(opMode)){
			 try {
         super.updateRecord(mapping, actionForm, request, response);
         this.commit(request);
         addMessage(request, MESSAGE_RECORD_UPDATE);
       } catch (ApplicationException appEx) {
         this.rollback(request);
         handleApplicationException(request, appEx);
       }
       if (isError(request)) {
      	 actionForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
         return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
       } else {
      	 actionForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
         return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
       }
		} else if (GlobalConstant.OP_MODE_DELETE.equals(opMode)){
			try {
        this.deleteRecord(mapping, actionForm, request, response);
        this.commit(request);
        actionForm.reset(mapping,request);
        addMessage(request, MESSAGE_RECORD_DELETE);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
      	actionForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
        return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
      } else {
      	actionForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
        return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
      }
		}
		return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
	}
}
